package com.ql.service.sbglService.impl;

import com.ql.dao.sheBeiGuanLi.buyMapper;
import com.ql.pojo.sheBeiGuanLi.Buy;
import com.ql.service.sbglService.buyService;
import com.ql.utils.PageHelper;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service("buyservice")
public class BuyServiceImpl implements buyService {
    private static final Logger logger=Logger.getLogger(BuyServiceImpl.class);
    @Resource
    private buyMapper buyMapper;
    //获取所有购买信息
    @Override
    public List<Buy> getAllBuy(PageHelper ph,String mohuName) {
        List<Buy> buyList=new ArrayList<Buy>();
        try {
            //add:更新总条数
            ph.setTotalCount(buyMapper.qryCount(mohuName));
            buyList=buyMapper.qryAll((ph.getCurrentPageNo()-1)*ph.getPageSize(),ph.getPageSize(),mohuName);
            if(buyList.size()<=0){
                logger.debug("BuyServiceImpl.getAllBuy查询为空！");
            }else{
                logger.debug("BuyServiceImpl.getAllBuy查询成功！");
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("BuyServiceImpl.getAllBuy查询异常！"+e.getMessage());
        }
        return buyList;
    }
    //单个删除
    @Override
    public boolean removeCGGL(int bId) {
        int row=0;
        try {
            row=buyMapper.del(bId);
            if(row !=1){
                logger.debug("BuyServiceImpl.removeCGGL 删除库存信息失败！");
                return false;
            }else{
                logger.debug("BuyServiceImpl.removeCGGL 删除库存信息成功！");
                return true;
            }
        } catch (Exception e) {
            logger.error("BuyServiceImpl.removeCGGL 删除库存信息异常》》》"+e.getMessage());
            return false;
        }
    }
    //批量删除
    @Override
    public boolean deleteBatch(List<Integer> list) {
        boolean result = false;//默认删除失败
        try {
            int x = buyMapper.delBatch(list);//x!=0就删除成功了
            if(x!=0){
                result = true;
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("BuyServiceImpl.deleteBatch删除异常！"+e.getMessage());
        }
        return result;
    }
    //获取id查询购买信息
    @Override
    public Buy getGMById(String bId) {
        Buy buy=null;
        try {
            buy=buyMapper.qryById(bId);
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("BuyServiceImpl.getKCById查询异常！"+e.getMessage());
            buy=new Buy();
        }
        return buy;
    }
    //新增购买信息
    @Override
    public boolean addNewGM(Buy buy) {
        int row=0;
        try {
            row=buyMapper.save(buy);
            if(row==1){
                logger.debug("BuyServiceImpl.addNewGM新增库存成功！");
                return true;
            }else {
                logger.debug("BuyServiceImpl.addNewGM新增库存失败！");
                return false;
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("BuyServiceImpl.addNewGM新增库存异常！"+e.getMessage());
            return false;
        }
    }
    //修改购买信息
    @Override
    public boolean updateGM(Buy buy) {
        boolean updateGM=false;
        try {
            int row=buyMapper.modify(buy);
            if(row==1){
                logger.debug("BuyServiceImpl.updateGM修改购买成功！");
                updateGM=true;
            }else {
                logger.debug("BuyServiceImpl.updateGM修改购买失败！");
                updateGM=false;
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("BuyServiceImpl.updateGM修改购买异常！"+e.getMessage());
            updateGM=false;
        }
        return updateGM;
    }
}
